package com.brainhealth.customer.service.impl;

import java.util.List;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.CustomerExercise;
import com.brainhealth.customer.domain.ExerciseOrder;
import com.brainhealth.customer.mapper.ExerciseOrderMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.ExerciseMapper;
import com.brainhealth.customer.domain.Exercise;
import com.brainhealth.customer.service.IExerciseService;

/**
 * 训练产品项目Service业务层处理
 * 
 * @author wolong
 * @date 2023-05-09
 */
@Service
public class ExerciseServiceImpl implements IExerciseService 
{
    /**
     * 日志
     */
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ExerciseOrderMapper exerciseOrderMapper;

    @Autowired
    private ExerciseMapper exerciseMapper;

    /**
     * 查询训练产品项目
     * 
     * @param exerciseId 训练产品项目主键
     * @return 训练产品项目
     */
    @Override
    public Exercise selectExerciseByExerciseId(Long exerciseId)
    {
        return exerciseMapper.selectExerciseByExerciseId(exerciseId);
    }

    /**
     * 查询训练产品项目列表
     * 
     * @param exercise 训练产品项目
     * @return 训练产品项目
     */
    @Override
    public List<Exercise> selectExerciseList(Exercise exercise)
    {
        return exerciseMapper.selectExerciseList(exercise);
    }

    /**
     * 新增训练产品项目
     * 
     * @param exercise 训练产品项目
     * @return 结果
     */
    @Override
    public int insertExercise(Exercise exercise)
    {
        exercise.setCreateTime(DateUtils.getNowDate());
        return exerciseMapper.insertExercise(exercise);
    }

    /**
     * 修改训练产品项目
     * 
     * @param exercise 训练产品项目
     * @return 结果
     */
    @Override
    public int updateExercise(Exercise exercise)
    {
        exercise.setUpdateTime(DateUtils.getNowDate());
        return exerciseMapper.updateExercise(exercise);
    }

    /**
     * 批量删除训练产品项目
     * 
     * @param exerciseIds 需要删除的训练产品项目主键
     * @return 结果
     */
    @Override
    public int deleteExerciseByExerciseIds(Long[] exerciseIds)
    {
        return exerciseMapper.deleteExerciseByExerciseIds(exerciseIds);
    }

    /**
     * 删除训练产品项目信息
     * 
     * @param exerciseId 训练产品项目主键
     * @return 结果
     */
    @Override
    public int deleteExerciseByExerciseId(Long exerciseId)
    {
        return exerciseMapper.deleteExerciseByExerciseId(exerciseId);
    }

    @Override
    public boolean getCompletedState(List<CustomerExercise> finishedCustomerExerciseList, List<CustomerExercise> expiredCustomerExerciseList) {
        if (CollectionUtils.isEmpty(finishedCustomerExerciseList) && CollectionUtils.isEmpty(expiredCustomerExerciseList)) {
            logger.warn("=======================================已完成&已过期都为空=======================================");
            return false;
        }
        int finishedSize = CollectionUtils.isEmpty(finishedCustomerExerciseList) ? 0 : finishedCustomerExerciseList.size();
        int expiredSize = CollectionUtils.isEmpty(expiredCustomerExerciseList) ? 0 : expiredCustomerExerciseList.size();
        int completedSize = finishedSize + expiredSize;
        CustomerExercise customerExercise = null;
        if (!CollectionUtils.isEmpty(finishedCustomerExerciseList)) {
            customerExercise = finishedCustomerExerciseList.get(0);
        }
        if (!CollectionUtils.isEmpty(expiredCustomerExerciseList)) {
            customerExercise = expiredCustomerExerciseList.get(0);
        }
        String orderId = customerExercise.getOrderId();
        ExerciseOrder exerciseOrder = exerciseOrderMapper.selectExerciseOrderByOrderId(orderId);
        Long exerciseProductId = exerciseOrder.getExerciseProductId();
        Exercise exercise = new Exercise();
        exercise.setExerciseProductId(exerciseProductId);
        List<Exercise> exerciseList = selectExerciseList(exercise);
        int exerciseSize = CollectionUtils.isEmpty(exerciseList) ? 0 : exerciseList.size();

        return completedSize == exerciseSize;
    }


}
